﻿namespace Gama.Website
{
    public class CartManager
    {
        ////private CartDto _Cart;
        ////public CartDto GetFromDB()
        ////{
        ////    var service = new CartService();
        ////    var cartDTO = new CartDto();
        ////    var visitor = VisitContext.Visitor;
        ////    try
        ////    {
        ////        cartDTO = visitor.IsAuthenticated
        ////                       ? service.GetCartByCustomerId(visitor.ID)
        ////                       : service.GetCartByGuestId(visitor.ID);
        ////        cartDTO = cartDTO ?? new CartDto { CustomerID = visitor.ID };
        ////    }
        ////    catch
        ////    {
        ////        cartDTO = new CartDto { CustomerID = visitor.ID };
        ////    }
        ////    return cartDTO;
        ////}
        ////public CartDto Get()
        ////{
        ////    var service = new CartService();
        ////    if (_Cart == null)
        ////    {
        ////        _Cart = GetFromDB();
        ////    }        
        ////    return _Cart;
        ////}
        ////public void Clear()
        ////{
        ////    //Delete Cart DB
        ////    new CartService().Delete(this._Cart.ID);
        ////    //Delete Cart session
        ////    VisitContext.SessionManager.Instance.CartManager = null;            
        ////}
        ////public CartManager CreateNew()
        ////{
        ////    var newCart = new CartDto { 
        ////       //ID = int.Newint(
        ////        CustomerID = VisitContext.Visitor.ID,
        ////        Price = Decimal.Zero,
        ////        Items = new List<CartItemDto>(),
        ////        CreatedDate = DateTime.Now
        ////    };
        ////    this._Cart = newCart;
        ////    return this;
        ////}
        ////public void AddOrUpdateItem(CartItemDto item)
        ////{
        ////    for(int i=0 ; i < this._Cart.Items.Count ; i ++)
        ////    {
        ////        if(this._Cart.Items[i].ProductID == item.ProductID)
        ////        {
        ////            var quantity= item.Quantity + this._Cart.Items[i].Quantity;                    
        ////            this._Cart.Items[i].Price = item.UnitPrice * quantity;
        ////            this._Cart.Items[i].Quantity = quantity;
        ////            return;
        ////        }
        ////    }
        ////    //Add new item
        ////    this._Cart.Items.Add(item);
        ////}
        ////public void Update()
        ////{
        ////    if (_Cart != null)
        ////    {                
        ////        var service = new CartService();
        ////        _Cart.Price = _Cart.Items.Sum(m => m.Price);
        ////        _Cart.Quantity = _Cart.Items.Sum(m => m.Quantity);
        ////        _Cart = service.UpdateCart(_Cart);
        ////    }
        ////}
        ////public void MergeCart()
        ////{
        ////    var visitor = VisitContext.Visitor;
        ////    if (visitor.IsAuthenticated) // customer visitor?
        ////    {
        ////        var cartService = new CartService();
        ////        Guid guestID = Guid.Empty;
        ////        if (VisitContext.Cookie.GuestId.HasValue) // guest Id also found at client side?
        ////        {
        ////            guestID = VisitContext.Cookie.GuestId.Value;
        ////        }
        ////        var cartGuest = cartService.GetCartByGuestId(guestID);

        ////        _Cart = VisitContext.CartManager.GetFromDB();//has been login //reset cart
        ////        if (!cartGuest.IsNull())
        ////        {
        ////            foreach (var item in cartGuest.Items)
        ////            {
        ////                this.AddOrUpdateItem(item);
        ////            }
        ////            //Delete Guest cart in DB
        ////            cartService.Delete(cartGuest.ID);
        ////        }                

        ////        this.Update();               
        ////    }                        
        ////}
    }
}